package drone.basis.express ;

import com.googlecode.aviator.runtime.function.AbstractFunction ;
import com.googlecode.aviator.runtime.function.FunctionUtils ;
import com.googlecode.aviator.runtime.type.AviatorDouble ;
import com.googlecode.aviator.runtime.type.AviatorObject ;

import java.nio.ByteBuffer ;
import java.nio.ByteOrder ;
import java.util.Map ;

public class FuncU32 extends AbstractFunction
{

	@Override
	public AviatorObject call (
			Map < String , Object > env ,
			AviatorObject arg1 ,
			AviatorObject arg2 ,
			AviatorObject arg3 ,
			AviatorObject arg4
	)
	{
		byte b1 = FunctionUtils.getNumberValue ( arg1 , env ).byteValue ( ) ;
		byte b2 = FunctionUtils.getNumberValue ( arg2 , env ).byteValue ( ) ;
		byte b3 = FunctionUtils.getNumberValue ( arg3 , env ).byteValue ( ) ;
		byte b4 = FunctionUtils.getNumberValue ( arg4 , env ).byteValue ( ) ;
		ByteBuffer bb = ByteBuffer.allocate ( 4 ).order ( ByteOrder.BIG_ENDIAN ) ;
		bb.put ( b1 ).put ( b2 ).put ( b3 ).put ( b4 ) ;
		return new AviatorDouble ( ( double ) ( bb.getInt ( 0 ) & 0xffffffff ) ) ;
	}

	public String getName ( )
	{
		return "u32" ;
	}
}
